package jiuxing

type PlugState struct {
	State       uint8
	WorkMode    uint8
	ConnectMode uint8
	ActMode     uint8
	Bleed       uint8
	Lock        uint8
	AuxState    uint8
	AuxV        uint8
	BMSComm     uint8
	CPSquare    uint8
	CCMode      uint8
	CPMode      uint8
}

type Msg09 struct {
	Tm      []byte
	Fun     uint8
	Heat    uint8
	PlugNum uint8
	Status  []PlugState
}

func (msg *Msg09) Decode(b []byte) {
	item := NewByteArray(b)
	msg.Tm = item.GetTimeArray(0)

	msg.Fun = item.GetUint8(6) | 0x01
	msg.Heat = (item.GetUint8(6) | 0x02) >> 1

	msg.PlugNum = item.GetUint8(8)
	for i := 0; i < int(msg.PlugNum); i++ {
		idx := 5*i + 9
		status := item.GetUint8(idx + 2)
		mod := item.GetUint8(idx + 3)
		plugState := PlugState{
			State:       item.GetUint8(idx),
			WorkMode:    item.GetUint8(idx + 1),
			ConnectMode: (status >> 0) & 0x01,
			ActMode:     (status >> 1) & 0x01,
			Bleed:       (status >> 2) & 0x01,
			Lock:        (status >> 3) & 0x01,
			AuxState:    (status >> 4) & 0x01,
			AuxV:        (status >> 5) & 0x01,
			BMSComm:     (status >> 6) & 0x01,
			CPSquare:    (status >> 7) & 0x01,
			CCMode:      mod & 0x0f,
			CPMode:      (mod >> 4) & 0x0f,
		}
		msg.Status = append(msg.Status, plugState)
	}
}
